package com.bookstore.common.util;

import cn.hutool.core.util.StrUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * IP工具类
 */
public class IpUtil {

    /**
     * 获取客户端IP
     * @param request 请求对象
     * @return IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request == null) {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (isInvalidIp(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (isInvalidIp(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (isInvalidIp(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (isInvalidIp(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (isInvalidIp(ip)) {
            ip = request.getRemoteAddr();
        }
        
        // 多个代理的情况，第一个IP为客户端真实IP
        if (ip != null && ip.length() > 15 && ip.indexOf(",") > 0) {
            ip = ip.substring(0, ip.indexOf(","));
        }
        
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
    
    /**
     * 判断IP是否无效
     * @param ip IP地址
     * @return 是否无效
     */
    private static boolean isInvalidIp(String ip) {
        return StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip);
    }
    
    /**
     * 获取设备类型
     * @param userAgent User-Agent
     * @return 设备类型：1-PC，2-移动端，3-平板
     */
    public static int getDeviceType(String userAgent) {
        if (StrUtil.isEmpty(userAgent)) {
            return 1;
        }
        userAgent = userAgent.toLowerCase();
        // 移动端
        if (userAgent.contains("mobile") || userAgent.contains("android") || userAgent.contains("iphone")) {
            return 2;
        }
        // 平板
        if (userAgent.contains("ipad") || userAgent.contains("tablet")) {
            return 3;
        }
        // PC
        return 1;
    }
    
    /**
     * 获取浏览器类型
     * @param userAgent User-Agent
     * @return 浏览器类型
     */
    public static String getBrowserType(String userAgent) {
        if (StrUtil.isEmpty(userAgent)) {
            return "unknown";
        }
        userAgent = userAgent.toLowerCase();
        if (userAgent.contains("edge")) {
            return "Edge";
        } else if (userAgent.contains("chrome")) {
            return "Chrome";
        } else if (userAgent.contains("firefox")) {
            return "Firefox";
        } else if (userAgent.contains("safari")) {
            return "Safari";
        } else if (userAgent.contains("opera")) {
            return "Opera";
        } else if (userAgent.contains("msie") || userAgent.contains("trident")) {
            return "IE";
        }
        return "unknown";
    }
    
    /**
     * 获取操作系统类型
     * @param userAgent User-Agent
     * @return 操作系统类型
     */
    public static String getOsType(String userAgent) {
        if (StrUtil.isEmpty(userAgent)) {
            return "unknown";
        }
        userAgent = userAgent.toLowerCase();
        if (userAgent.contains("windows")) {
            return "Windows";
        } else if (userAgent.contains("mac os")) {
            return "MacOS";
        } else if (userAgent.contains("linux")) {
            return "Linux";
        } else if (userAgent.contains("android")) {
            return "Android";
        } else if (userAgent.contains("iphone") || userAgent.contains("ipad")) {
            return "iOS";
        }
        return "unknown";
    }
}